package com.ssy.lingxi.after.sale.controller;

import com.ssy.lingxi.after.sale.api.model.constant.AgentFlagEnum;
import com.ssy.lingxi.after.sale.api.model.vo.request.CommonIdVO;
import com.ssy.lingxi.after.sale.api.model.vo.request.VerifyVO;
import com.ssy.lingxi.after.sale.model.vo.request.*;
import com.ssy.lingxi.after.sale.model.vo.response.*;
import com.ssy.lingxi.after.sale.service.IRepairGoodsDetailService;
import com.ssy.lingxi.after.sale.service.IRepairGoodsService;
import com.ssy.lingxi.common.controller.BaseController;
import com.ssy.lingxi.common.model.dto.UserLoginCacheDTO;
import com.ssy.lingxi.common.response.PageData;
import com.ssy.lingxi.common.response.Wrapper;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

/**
 * @menu 售后能力-售后维修
 * @author 伍将
 * @date 2020/9/3
 * @version 2.0.0
 */
@RestController
@RequestMapping("/as/repairGoods")
public class RepairGoodsController extends BaseController {

    @Resource
    private IRepairGoodsService repairApplyService;

    @Resource
    private IRepairGoodsDetailService repairGoodsService;

    /**
     * 售后维修申请-维修申请单查询-分页查询维修申请
     * @author 伍将
     * @date 2020/9/3
     **/
    @RequestMapping(value = "/pageByConsumer", method = RequestMethod.GET)
    public Wrapper<PageData<RepairGoodsQueryVO>> pageByConsumer(HttpServletRequest httpRequest, @Valid RepairGoodsPageByConsumerVO pageVO) {
        return repairApplyService.pageByConsumer(getSysUser(httpRequest), pageVO);
    }

    /**
     * 售后维修申请-维修申请单查询-分页查询维修申请-im
     * @author ds
     * @date 2020/4/22
     **/
    @RequestMapping(value = "/pageByIM", method = RequestMethod.GET)
    public Wrapper<PageData<AfterSaleGoodsIMQueryVO>> pageByIM(HttpServletRequest httpRequest, @Valid AfterSaleGoodsIMPageVO pageVO) {
        return repairApplyService.pageByIM(getSysUser(httpRequest), pageVO);
    }

    /**
     * 售后维修申请-查询维修申请详情
     * @author 伍将
     * @date 2020/9/3
     **/
    @RequestMapping(value = "/getDetailByConsumer", method = RequestMethod.GET)
    public Wrapper<RepairGoodsDetailVO> getDetailByConsumer(HttpServletRequest httpRequest, Long repairId) {
        return repairApplyService.getDetailByConsumer(getSysUser(httpRequest), repairId);
    }

    /**
     * 售后维修-维修申请详情-分页查询维修货物
     * @param pageVO 分页查询条件
     * @return 分页查询维修货物
     */
    @RequestMapping(value = "/pageRepairGoods", method = RequestMethod.GET)
    public Wrapper<PageData<RepairGoodsDetailQueryVO>> pageList(@Valid RepairGoodsDetailPageVO pageVO) {
        return repairGoodsService.pageList(pageVO);
    }


    /**
     * 售后维修申请-待提交维修申请单-分页查询维修申请
     * @author 伍将
     * @date 2020/9/3
     **/
    @RequestMapping(value = "/pageToBeSubmitByConsumer", method = RequestMethod.GET)
    public Wrapper<PageData<RepairGoodsQueryVO>> pageToBeSubmitByConsumer(HttpServletRequest httpRequest, @Valid RepairGoodsPageByConsumerVO pageVO) {
        return repairApplyService.pageToBeSubmitByConsumer(getSysUser(httpRequest), pageVO);
    }

    /**
     * 售后维修申请-待提交维修申请单-提交维修申请单-新增/修改维修申请
     * @author 伍将
     * @date 2020/9/3
     **/
    @RequestMapping("/save")
    public Wrapper<Long> save(HttpServletRequest httpRequest, @RequestBody @Valid RepairGoodsSaveVO saveVO) {
        return repairApplyService.save(getSysUser(httpRequest), saveVO);
    }

    /**
     * 售后代客维修申请-待提交维修申请单-提交维修申请单-新增/修改维修申请
     * @author 彭冬冬
     * @date 2021/11/25
     * @param httpRequest 请求
     * @param saveVO 保存代客维修VO
     * @return 售后维修申请id
     */
    @RequestMapping("/agent/save")
    public Wrapper<Long> agentSave(HttpServletRequest httpRequest, @RequestBody @Valid RepairGoodsAgentSaveVO saveVO) {
        UserLoginCacheDTO user = buildAgentUser(httpRequest, saveVO);
        saveVO.setAgentFlag(AgentFlagEnum.YES.getCode());
        return repairApplyService.save(user, saveVO);
    }

    /**
     * 构建代客用户
     * @author 彭冬冬
     * @date 2021/11/25
     * @param httpRequest 请求
     * @param saveVO 保存代客维修VO
     * @return 代客用户
     */
    private UserLoginCacheDTO buildAgentUser(HttpServletRequest httpRequest, RepairGoodsAgentSaveVO saveVO) {
        // 校验
        getSysUser(httpRequest);
        // 创建
        UserLoginCacheDTO user = new UserLoginCacheDTO();
        user.setMemberId(saveVO.getMemberId());
        user.setMemberRoleId(saveVO.getMemberRoleId());
        user.setCompany(saveVO.getCompany());
        return user;
    }

    /**
     * 售后维修申请-待提交维修申请单-删除维修申请
     * @author 伍将
     * @date 2020/9/3
     **/
    @RequestMapping("/delete")
    public Wrapper<Void> delete(HttpServletRequest httpRequest, @RequestBody CommonIdVO commonIdVO) {
        return repairApplyService.delete(getSysUser(httpRequest), commonIdVO.getDataId());
    }

    /**
     * 售后维修申请-待提交维修申请单-提交维修申请
     * @author 伍将
     * @date 2020/9/3
     **/
    @RequestMapping("/submit")
    public Wrapper<Void> submit(HttpServletRequest httpRequest, @RequestBody CommonIdVO commonIdVO) {
        return repairApplyService.submit(getSysUser(httpRequest), commonIdVO.getDataId());
    }

    /**
     * 售后维修申请-待确认售后完成-分页查询维修申请
     * @author 伍将
     * @date 2020/9/3
     **/
    @RequestMapping(value = "/pageToBeConfirmComplete", method = RequestMethod.GET)
    public Wrapper<PageData<RepairGoodsQueryVO>> pageToBeConfirmComplete(HttpServletRequest httpRequest, @Valid RepairGoodsPageByConsumerVO pageVO) {
        return repairApplyService.pageToBeConfirmComplete(getSysUser(httpRequest), pageVO);
    }

    /**
     * 售后维修申请-待确认售后完成-确认售后完成
     * @author 伍将
     * @date 2020/9/3
     **/
    @RequestMapping("/confirmComplete")
    public Wrapper<Void> confirmComplete(HttpServletRequest httpRequest, @RequestBody @Valid RepairGoodsConfirmCompleteVO completeVO) {
        return repairApplyService.confirmComplete(getSysUser(httpRequest), completeVO);
    }

    /**
     * 售后维修处理-维修申请单查询-分页查询维修申请
     * @author 伍将
     * @date 2020/9/3
     **/
    @RequestMapping(value = "/pageBySupplier", method = RequestMethod.GET)
    public Wrapper<PageData<RepairGoodsQueryVO>> pageBySupplier(HttpServletRequest httpRequest, @Valid RepairGoodsPageBySupplierVO pageVO) {
        return repairApplyService.pageBySupplier(getSysUser(httpRequest), pageVO);
    }

    /**
     * 售后维修处理-查询维修申请详情
     * @author 伍将
     * @date 2020/9/3
     **/
    @RequestMapping(value = "/getDetailBySupplier", method = RequestMethod.GET)
    public Wrapper<RepairGoodsDetailVO> getDetailBySupplier(HttpServletRequest httpRequest, Long repairId) {
        return repairApplyService.getDetailBySupplier(getSysUser(httpRequest), repairId);
    }

    /**
     * 售后维修处理-待提交审核维修申请单-分页查询维修申请
     * @author 伍将
     * @date 2020/9/3
     **/
    @RequestMapping(value = "/pageToBeSubmitBySupplier", method = RequestMethod.GET)
    public Wrapper<PageData<RepairGoodsQueryVO>> pageToBeSubmitBySupplier(HttpServletRequest httpRequest, @Valid RepairGoodsPageBySupplierVO pageVO) {
        return repairApplyService.pageToBeSubmitBySupplier(getSysUser(httpRequest), pageVO);
    }

    /**
     * 售后维修处理-待提交审核维修申请单-提交审核维修申请
     * @author 伍将
     * @date 2020/9/3
     **/
    @RequestMapping("/submitVerify")
    public Wrapper<Void> submitVerify(HttpServletRequest httpRequest, @RequestBody @Valid VerifyVO verifyVO) {
        return repairApplyService.submitVerify(getSysUser(httpRequest), verifyVO);
    }

    /**
     * 售后维修处理-待审核维修申请单(一级)-分页查询维修申请
     * @author 伍将
     * @date 2020/9/3
     **/
    @RequestMapping(value = "/pageToBeVerifyStepOne", method = RequestMethod.GET)
    public Wrapper<PageData<RepairGoodsQueryVO>> pageToBeVerifyStepOne(HttpServletRequest httpRequest, @Valid RepairGoodsPageBySupplierVO pageVO) {
        return repairApplyService.pageToBeVerifyStepOne(getSysUser(httpRequest), pageVO);
    }

    /**
     * 售后维修处理-待审核维修申请单(一级)-审核维修申请
     * @author 伍将
     * @date 2020/10/17
     **/
    @RequestMapping("/verifyStepOne")
    public Wrapper<Void> verifyStepOne(HttpServletRequest httpRequest, @RequestBody @Valid VerifyVO verifyVO) {
        return repairApplyService.verifyStepOne(getSysUser(httpRequest), verifyVO);
    }

    /**
     * 售后维修处理-待审核维修申请单(二级)-分页查询维修申请
     * @author 伍将
     * @date 2020/9/3
     **/
    @RequestMapping(value = "/pageToBeVerifyStepTwo", method = RequestMethod.GET)
    public Wrapper<PageData<RepairGoodsQueryVO>> pageToBeVerifyStepTwo(HttpServletRequest httpRequest, @Valid RepairGoodsPageBySupplierVO pageVO) {
        return repairApplyService.pageToBeVerifyStepTwo(getSysUser(httpRequest), pageVO);
    }

    /**
     * 售后维修处理-待审核维修申请单(二级)-审核维修申请
     * @author 伍将
     * @date 2020/10/17
     **/
    @RequestMapping("/verifyStepTwo")
    public Wrapper<Void> verifyStepTwo(HttpServletRequest httpRequest, @RequestBody @Valid VerifyVO verifyVO) {
        return repairApplyService.verifyStepTwo(getSysUser(httpRequest), verifyVO);
    }

    /**
     * 售后维修处理-待确认维修申请单-分页查询维修申请
     * @author 伍将
     * @date 2020/9/3
     **/
    @RequestMapping(value = "/pageToBeConfirm", method = RequestMethod.GET)
    public Wrapper<PageData<RepairGoodsQueryVO>> pageToBeConfirm(HttpServletRequest httpRequest, @Valid RepairGoodsPageBySupplierVO pageVO) {
        return repairApplyService.pageToBeConfirm(getSysUser(httpRequest), pageVO);
    }

    /**
     * 售后维修处理-待确认维修申请单-确认审核维修申请
     * @author 伍将
     * @date 2020/9/3
     **/
    @RequestMapping("/confirmVerify")
    public Wrapper<Void> confirmVerify(HttpServletRequest httpRequest, @RequestBody @Valid VerifyVO verifyVO) {
        return repairApplyService.confirmVerify(getSysUser(httpRequest), verifyVO);
    }

    /**
     * 分页查询列表页面搜索条件内容
     * @author 伍将
     * @date 2020/10/17
     **/
    @RequestMapping(value = "/pageItems", method = RequestMethod.GET)
    public Wrapper<RepairGoodsPageItemsVO> pageItems(HttpServletRequest httpRequest) {
        return repairApplyService.pageItems(getSysUser(httpRequest));
    }

    /**
     * 平台后台-维修申请单管理-分页查询维修申请
     * @author 伍将
     * @date 2020/12/7
     **/
    @RequestMapping(value = "/pageByPlatform", method = RequestMethod.GET)
    public Wrapper<PageData<RepairGoodsQueryVO>> pageByPlatform(HttpServletRequest httpRequest, @Valid RepairGoodsPageByPlatformVO pageVO) {
        return repairApplyService.pageByPlatform(getSysUser(httpRequest), pageVO);
    }

    /**
     * 平台后台-维修申请单管理-查询维修申请详情
     * @author 伍将
     * @date 2020/12/7
     **/
    @RequestMapping(value = "/getDetailByPlatform", method = RequestMethod.GET)
    public Wrapper<RepairGoodsDetailVO> getDetailByPlatform(HttpServletRequest httpRequest, Long repairId) {
        return repairApplyService.getDetailByPlatform(getSysUser(httpRequest), repairId);
    }
}
